package com.mitgroup.dmrvs.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mitgroup.dmrvs.R;
import com.mitgroup.dmrvs.adapter.ImageAdapter;
import com.mitgroup.dmrvs.utils.AppManager;
import com.mitgroup.dmrvs.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.PreferencesUtils;

public class RecommendProductsActivity extends AppCompatActivity {
    @BindView(R.id.btn_back)
    public Button btnBack;
    @OnClick(R.id.btn_back)
    public void onBack(View view) {
        this.finish();
    }
    @BindView(R.id.tv_user_name)
    public TextView tvUserName;
    @BindView(R.id.iv_quit)
    public Button quit;
    @OnClick(R.id.iv_quit)
    public void onQuit(View view) {
        Utils.Exit(this);
    }
    @BindView(R.id.gridView)
    public GridView gridView;
    @BindView(R.id.scroller)
    public ExtendedHorizontalScrollView scroller;
    private int[] imageIds;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend_products);
        ButterKnife.bind(this);
        initView();

    }
    private void initView() {
        AppManager.getAppManager().addActivity(this);
        String userName= PreferencesUtils.getString(RecommendProductsActivity.this,"userName");
        tvUserName.setText(userName);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // heigth = dm.heightPixels;
        int  width = dm.widthPixels;
        int scrollWidth=width-150;
        scroller.getLayoutParams().width=scrollWidth;

        ((ExtendedHorizontalScrollView)findViewById(R.id.scroller)).setScrollStateListener(new ExtendedHorizontalScrollView.IScrollStateListener() {
            public void onScrollMostRight() {
                ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.INVISIBLE);
            }

            public void onScrollMostLeft() {
                ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.INVISIBLE);
            }

            public void onScrollFromMostLeft() {
                ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.VISIBLE);
            }

            public void onScrollFromMostRight() {
                ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.VISIBLE);
            }

        });
        Intent intent = getIntent();
        String product = intent.getStringExtra("product");
        if(!" ".equals(product) && product!=null && !"null".equals(product))
        {
            String productArr[]=product.split(",");
            imageIds=new int[productArr.length];
            String productStr="";
            for (int i=0;i<productArr.length;i++)
            {
                productStr=productArr[i];
                if("wa11000".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa11000;
                }
                else if("wa66101".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa66101;
                }
                else if("wa68102".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa68102;
                }
                else if("wa60802".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa60802;
                }
                else if("wa61510".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa61510;
                }
                else if("wa64401".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa64401;
                }
                else if("wa64506".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa64506;
                }
                else if("waa4701".equals(productStr))
                {
                    imageIds[i]=R.drawable.waa4701;
                }
                else if("waa4601".equals(productStr))
                {
                    imageIds[i]=R.drawable.waa4601;
                }
                else if("wa62409".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa62409;
                }
                else if("wa64805".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa64805;
                }
                else if("wa66701".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa66701;
                }
                else if("wa30208".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa30208;
                }
                else if("wa62801".equals(productStr))
                {
                    imageIds[i]=R.drawable.wa62801;
                }
            }
        }
        else
        {
            //imageIds = new int[] { R.drawable.ic_launcher};
        }

        gridView.setAdapter(new ImageAdapter(this,imageIds));
        //事件监听
//	          gridView.setOnItemClickListener(new OnItemClickListener(){
//	              public void onItemClick(AdapterView<?> parent, View view,
//	                      int position, long id) {
//
//	              }
//	          });
        setHorizontalGridView(imageIds.length,gridView);


    }
    private void setHorizontalGridView(int size, GridView gridView) {
        int length = size;
        //一个界面要显示的几个Item
        int AnInterfaceNum=2;
        //每个Item的间距(注:如果间距过大,但屏幕宽度不够,多出的部份会被无视)
        int spcing =2;
        //计算当个Item的宽度( 屏幕宽度 减去- 一个屏幕下要总item数量的间距之和 最后除/ 单个屏幕要显示几个Item)
        int itemWidth = ((getResources().getDisplayMetrics().widthPixels) - ((AnInterfaceNum - 1) * spcing)) / AnInterfaceNum;
        //    float density = dm.density;
        //
        //    int gridviewWidth = (int) (size * (length) * density)+((size-1)*30);
        //    int itemWidth = (int) ((length) * density);
        //GridView总长度
        int gridviewWidth = (length * (itemWidth)) + ((length - 1) * spcing);

        @SuppressWarnings("deprecation")
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        gridView.setLayoutParams(params);      // 设置GirdView布局参数,横向布局的关键
        gridView.setColumnWidth(itemWidth);     // 设置列表项宽
        gridView.setHorizontalSpacing(spcing);   // 设置列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(length);       // 设置列数量=列表集合数

    }

    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }



}
